package mx.utils.weatherUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.util.EntityUtils;

import com.sun.tools.extcheck.Main;

import mx.utils.DateJsonValueProcessor;
import mx.utils.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class WeatherUtil {

	public static String getWeatherbycityname(String cityname) throws Exception {
		String host = "http://jisutqybmf.market.alicloudapi.com";
		String path = "/weather/query";
		String method = "GET";
		String appcode = "44b70529cb094b4a83d253989d1981df";
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Authorization", "APPCODE " + appcode);
		Map<String, String> querys = new HashMap<String, String>();
		querys.put("city", cityname);
		querys.put("citycode", "citycode");
		querys.put("cityid", "cityid");
		querys.put("ip", "ip");
		querys.put("location", "location");
		HttpResponse response = null;
		response = HttpUtils.doGet(host, path, method, headers, querys);

		return EntityUtils.toString(response.getEntity());
	}

	public static void main(String[] args) throws Exception {

		String z = WeatherUtil.getWeatherbycityname("杭州");
		JSONObject result = new JSONObject();
		JSONObject result2 = new JSONObject();

		String a = result2.fromObject(result.fromObject(z).get("result")).toString();

		int first = 0;
		Matcher matcher = Pattern.compile("update").matcher(a);
		if (matcher.find()) {
			first = matcher.start();
		} else {
			first = -1;
		}

		String last = a.substring(1, first - 2);
		String[] lasetarray = last.split(",");
		Map map = new HashMap();
		for (int i = 0; i < lasetarray.length; i++) {
			String string = lasetarray[i];
			String[] x = string.split(":");
			map.put(x[0], x[1]);
		}

		System.out.println(map);
	}
}
